package jcolibri.test.recomendador.presentacion;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.util.Collection;
import java.util.Vector;

import jcolibri.cbrcore.Attribute;
import jcolibri.cbrcore.CBRCase;
import jcolibri.cbrcore.CaseComponent;
import jcolibri.util.AttributeUtils;


public class SimpleTable2 extends JFrame {

	private Collection<CBRCase> casos;
	
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;

	
	private static Vector getAttributes(CBRCase c)
    {
	Vector<Object> res = new Vector<Object>();
	
	JRadioButton rb = new JRadioButton(c.getID().toString());
	res.add(rb);
	
	getAttributes(c.getDescription(), res);
	getAttributes(c.getSolution(), res);
	getAttributes(c.getJustificationOfSolution(), res);
	getAttributes(c.getResult(), res);
	
	return res;
    }
    
	
    private static void getAttributes(CaseComponent cc, Vector<Object> res)
    {
	Collection<Attribute> atts = AttributeUtils.getAttributes(cc);
	if(atts == null)
	    return;

	Attribute id = cc.getIdAttribute();
	for(Attribute a: atts)
	{
	    if(!a.equals(id))
	    {
	 /*   	Object o = AttributeUtils.findValue(a, cc);
	    	String genero = "";
	    	if (o!=null)
	    		if (o.getClass().getName().equals("java.lang.Boolean") && o.toString().equals("true"))
	    			genero = a.getName();
	   */ 	res.add(AttributeUtils.findValue(a, cc));
	    }
	}
    }
    
    /**
     * Obtains the column names of the tables from a case.
     * (It obtains the names of the attributes)
     * @param c is any case.
     * @return a list of objects
     */
    private static Vector<Object> extractColumnNames(CBRCase c)
    {
	Vector<Object> res = new Vector<Object>();
	res.add("Selecciona");
	extractColumnNames(c.getDescription(),res);
	extractColumnNames(c.getSolution(),res);
	extractColumnNames(c.getJustificationOfSolution(),res);
	extractColumnNames(c.getResult(),res);
	return res;
    }
    
    /**
     * Extracts the column names (names of the attributes) from
     * a CaseComponent.
     * @param cc is the CaseComponent.
     * @param res List to fill.
     */
    private static void extractColumnNames(CaseComponent cc, Vector<Object> res)
    {
	Collection<Attribute> atts = AttributeUtils.getAttributes(cc);
	if(atts == null)
	    return;
	Attribute id = cc.getIdAttribute();
	for(Attribute a: atts)
	{
	    if(!a.equals(id))
		res.add(a.getName());
	}
    }

	
	
	
	
	public SimpleTable2(Collection<CBRCase> cases) {
		this.casos=cases;

		Vector<Object> columnNames = extractColumnNames(casos.iterator().next());
		

		Vector<Object> rows = new Vector<Object>();
		for(CBRCase c: casos)
		    rows.add(getAttributes(c));

        
        
        //creamos el Modelo de la tabla con los datos anteriores
        DefaultTableModel dtm= new DefaultTableModel(rows, columnNames);
        //se crea la Tabla con el modelo DefaultTableModel
        final JTable table = new JTable(dtm);

        //se define el tamaño
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));


        //Creamos un JscrollPane y le agregamos la JTable
        JScrollPane scrollPane = new JScrollPane(table);

        //Agregamos el JScrollPane al contenedor
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(getJButton1(), BorderLayout.SOUTH);
        getContentPane().add(getJButton2(), BorderLayout.SOUTH);
        getContentPane().add(getJButton3(), BorderLayout.SOUTH);
        getContentPane().add(getJButton4(), BorderLayout.SOUTH);
        

        //manejamos la salida
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Aceptar película recomendada");
			jButton1.addActionListener(new oyenteOK());
			jButton1.setBounds(new Rectangle(50, 140, 100, 25));
		}
		return jButton1;
	}
	
	class oyenteOK implements ActionListener {
    	
        public void actionPerformed(ActionEvent a) {
        	setVisible(false);
        }
}
	

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Votar la pelicula seleccionada");
			jButton2.addActionListener(new oyenteVotar());
			jButton2.setBounds(new Rectangle(50, 170, 100, 25));
		}
		return jButton2;
	}

	class oyenteVotar implements ActionListener {
    	
        public void actionPerformed(ActionEvent a) {
        	setVisible(false);
        	Alta.getInstanciaVisible();
        }
}

	
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("Refinar por similitud de género");
			jButton3.addActionListener(new oyenteSimilares());
			jButton3.setBounds(new Rectangle(50, 200, 100, 25));
		}
		return jButton3;
	}
	
	class oyenteSimilares implements ActionListener {
    	
        public void actionPerformed(ActionEvent a) {
        	setVisible(false);
        }
}
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setText("Más Recomendaciones");
			jButton4.addActionListener(new oyenteMas());
			jButton4.setBounds(new Rectangle(50, 230, 100, 25));
		}
		return jButton4;
	}
	
	class oyenteMas implements ActionListener {
    	
        public void actionPerformed(ActionEvent a) {
        	setVisible(false);
        }
}

	
	
	
	
	
	public static void main(String[] args) {
//        SimpleTable2 frame = new SimpleTable2();
//        frame.pack();
//        frame.setVisible(true);
    }
}
